const app = getApp()
Page({
  data: {
    screenHeight: 0,
    newUser: false,
    screenHeight1: 0,
    filter: 0,
    secretDis: 'none',
    userinfo: '',
  },
  onLoad: async function (e) {
    app.receiverUnRead()
    console.log("加载了")
    var that = this
    wx.connectSocket({
      url: 'ws://47.96.87.172:8077/mf/ws/asset',
      header: {
        'content-type': 'application/json'
      },
      // protocols: ['protocol1'],
      success: res => {
        console.log("成功", res)
      },
      fail: res => {
        console.log(res)
      }
    })
    wx.onSocketMessage(function (res) {
      console.log("收到服务器内容：" + res.data)
      if(res.data!=''){
        wx.User.listContact()
        app.globalData.contactList = wx.getStorageSync('contactList')
      }
      if (!that.data.isSendUid) {
        new Promise((resolve, reject) => {
          resolve(wx.getStorageSync('userinfo').uid)
        }).then(res => {
          wx.sendSocketMessage({
            data: res + '',
          })
          that.setData({
            isSendUid: true
          })
        })
      }
    })
    // 判断是否授权
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              var avatarUrl = res.userInfo.avatarUrl
              // 登录
              wx.login({
                success: res => {
                  // 发送 res.code 到后台换取 openId, sessionKey, unionId
                  wx.User.login({
                    url: "/auth/login",
                    data: {
                      code: res.code,
                      avatar: avatarUrl
                    }
                  })
                }
              })
            }
          })
          wx.switchTab({
            url: '/pages/home/home',
          })
        } else {
          // 新用户
          this.setData({
            newUser: true
          })
        }
      }
    })
    this.setData({
      screenHeight: wx.getSystemInfoSync().windowHeight,
      screenHeight1: wx.getSystemInfoSync().windowHeight,
    }, () => {
      var k = that;
      setTimeout(function () {
        that.setData({
          screenHeight1: k.data.screenHeight * 0.66,
        })
      }, 500)
    })
  },
  toHome: function (e) {
    wx.switchTab({
      url: '../teacher/sign_home',
    })
  },

  getUserInfo: function (e) {
    if (e.detail.rawData == null) {
      //用户没按允许授权按钮
      this.setData({
        score_tea: '#F8F8F8'
      })
      wx.showToast({
        title: '请授权！',
        icon: 'none'
      })
    } else {
      //用户按允许授权按钮
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            this.go()
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          }
        }
      })
    }
  },

  go: function () {
    if (this.data.newUser) {
      this.setData({
        filter: 10,
        secretDis: 'flex'
      })
    }
  },
  handleEventListener: function (e) {
    wx.showLoading({
      title: '进入中',
    })
    if (e.detail.agree) {
      // 登录
      wx.login({
        success: res => {
          var code = res.code
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              wx.setStorageSync('avatarInfo', res.userInfo)
              app.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
              new Promise(async (resolve, reject) => {
                await wx.User.login({
                  url: "/auth/login",
                  data: {
                    code: code,
                    userAvatar: app.globalData.userInfo.avatarUrl
                  }
                })
                resolve(wx.getStorageSync('userinfo').type)
              }).then(res => {
                wx.switchTab({
                  url: '/pages/home/home',
                })
                wx.hideLoading({
                  success: (res) => {},
                })
              })
            }
          })
        }
      })
    } else {
      this.setData({
        secretDis: 'none',
        filter: 0
      })
    }
  }
})